<?php

class CommonAction extends AuthAction{
public function index() {
$map = $this->_search ();
if($this->get('default_map'))
$map = array_merge($map,$this->get('default_map'));
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model = D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
protected function _search($name = '') {
if (empty ( $name )) {
$name = $this->getActionName();
}
$name=$this->getActionName();
$model = D ( $name );
$map = array ();
foreach ( $model->getDbFields () as $key =>$val ) {
if (isset ( $_REQUEST [$val] ) &&$_REQUEST [$val] != '') {
$map [$val] = $_REQUEST [$val];
}
}
return $map;
}
protected function _list($model,$map,$sortBy = '',$asc = false) {
if (isset ( $_REQUEST ['_order'] )) {
$order = $_REQUEST ['_order'];
}else {
$order = !empty ( $sortBy ) ?$sortBy : $model->getPk ();
}
if (isset ( $_REQUEST ['_sort'] )) {
$sort = $_REQUEST ['_sort'] ?'asc': 'desc';
}else {
$sort = $asc ?'asc': 'desc';
}
$count = $model->where ( $map )->count ( 'id');
if ($count >0) {
if (!empty ( $_REQUEST ['listRows'] )) {
$listRows = $_REQUEST ['listRows'];
}else {
$listRows = '';
}
$p = new Page ( $count,$listRows );
$voList = $model->where($map)->order( '`'.$order .'` '.$sort)->limit($p->firstRow .','.$p->listRows)->findAll ( );
foreach ( $map as $key =>$val ) {
if (!is_array ( $val )) {
$p->parameter .= "$key=".urlencode ( $val ) .'&';
}
}
$page = $p->show ();
$sortImg = $sort;
$sortAlt = $sort == 'desc'?l('ASC_SORT') : l('DESC_SORT');
$sort = $sort == 'desc'?1 : 0;
$this->assign ( 'list',$voList );
$this->assign ( 'sort',$sort );
$this->assign ( 'order',$order );
$this->assign ( 'sortImg',$sortImg );
$this->assign ( 'sortType',$sortAlt );
$this->assign ( 'page',$page );
$this->assign ( 'nowPage',$p->nowPage);
}
return;
}
protected function uploadImage()
{
if(conf('WATER_MARK')!='')
$water_mark = get_real_path().conf('WATER_MARK');
else
$water_mark = '';
$alpha = conf('WATER_ALPHA');
$place = conf('WATER_POSITION');
$upload = new UploadFile();
$upload->maxSize  = conf('MAX_IMAGE_SIZE') ;
$upload->allowExts  =  explode(',',conf('ALLOW_IMAGE_EXT'));
$dir_name = to_date(get_gmtime(),'Ym');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$dir_name = $dir_name.'/'.to_date(get_gmtime(),'d');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$dir_name = $dir_name.'/'.to_date(get_gmtime(),'H');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$save_rec_Path = '/public/attachment/'.$dir_name.'/origin/';
$savePath = APP_ROOT_PATH.'public/attachment/'.$dir_name.'/origin/';
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name.'/origin/')) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name.'/origin/');
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name.'/origin/',0777);
}
$domain_path = get_domain().APP_ROOT.$save_rec_Path;
$upload->saveRule = 'uniqid';
$upload->savePath = $savePath;
if($upload->upload())
{
$uploadList = $upload->getUploadFileInfo();
foreach($uploadList as $k=>$fileItem)
{
$file_name = $fileItem['savepath'].$fileItem['savename'];
$big_save_path = str_replace('origin/','',$savePath);
$big_file_name = str_replace('origin/','',$file_name);
@file_put_contents($big_file_name,@file_get_contents($file_name));
if(file_exists($water_mark))
{
Image::water($big_file_name,$water_mark,$big_file_name,$alpha,$place);
}
$big_save_rec_Path = str_replace('origin/','',$save_rec_Path);
$uploadList[$k]['recpath'] = $save_rec_Path;
$uploadList[$k]['bigrecpath'] = $big_save_rec_Path;
if(app_conf('PUBLIC_DOMAIN_ROOT')!='')
{
$origin_syn_url = app_conf('PUBLIC_DOMAIN_ROOT').'/es_file.php?username='.app_conf('IMAGE_USERNAME').'&password='.app_conf('IMAGE_PASSWORD').'&file='.get_domain().APP_ROOT.'/public/attachment/'.$dir_name.'/origin/'.$fileItem['savename'].'&path=attachment/'.$dir_name.'/origin/&name='.$fileItem['savename'].'&act=0';
$big_syn_url = app_conf('PUBLIC_DOMAIN_ROOT').'/es_file.php?username='.app_conf('IMAGE_USERNAME').'&password='.app_conf('IMAGE_PASSWORD').'&file='.get_domain().APP_ROOT.'/public/attachment/'.$dir_name.'/'.$fileItem['savename'].'&path=attachment/'.$dir_name.'/&name='.$fileItem['savename'].'&act=0';
@file_get_contents($origin_syn_url);
@file_get_contents($big_syn_url);
}
}
return array('status'=>1,'data'=>$uploadList,'info'=>L('UPLOAD_SUCCESS'));
}
else 
{
return array('status'=>0,'data'=>null,'info'=>$upload->getErrorMsg());
}
}
protected function uploadFile()
{
$upload = new UploadFile();
$upload->maxSize  = conf('MAX_IMAGE_SIZE') ;
$upload->allowExts  =  explode(',',conf('ALLOW_IMAGE_EXT'));
$dir_name = to_date(get_gmtime(),'Ym');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$dir_name = $dir_name.'/'.to_date(get_gmtime(),'d');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$dir_name = $dir_name.'/'.to_date(get_gmtime(),'H');
if (!is_dir(APP_ROOT_PATH.'public/attachment/'.$dir_name)) {
@mkdir(APP_ROOT_PATH.'public/attachment/'.$dir_name);
@chmod(APP_ROOT_PATH.'public/attachment/'.$dir_name,0777);
}
$save_rec_Path = '/public/attachment/'.$dir_name.'/';
$savePath = APP_ROOT_PATH.'public/attachment/'.$dir_name.'/';
$domain_path = get_domain().APP_ROOT.$save_rec_Path;
$upload->saveRule = 'uniqid';
$upload->savePath = $savePath;
if($upload->upload())
{
$uploadList = $upload->getUploadFileInfo();
foreach($uploadList as $k=>$fileItem)
{
$uploadList[$k]['recpath'] = $save_rec_Path;
if(app_conf('PUBLIC_DOMAIN_ROOT')!='')
{
$syn_url = app_conf('PUBLIC_DOMAIN_ROOT').'/es_file.php?username='.app_conf('IMAGE_USERNAME').'&password='.app_conf('IMAGE_PASSWORD').'&file='.$domain_path.$fileItem['savename'].'&path=attachment/'.$dir_name.'/&name='.$fileItem['savename'].'&act=0';
@file_get_contents($syn_url);
}
}
return array('status'=>1,'data'=>$uploadList,'info'=>L('UPLOAD_SUCCESS'));
}
else 
{
return array('status'=>0,'data'=>null,'info'=>$upload->getErrorMsg());
}
}
public function _before_update()
{
$uname = $_REQUEST['uname'];
if($uname&&trim($uname)!='')
{
$rs = M(MODULE_NAME)->where("uname='".$uname."' and id <> ".intval($_REQUEST['id']))->count();
if($rs >0)
{
$this->error(l('UNAME_EXISTS'));
}
}
}
public function _before_insert()
{
$uname = $_REQUEST['uname'];
if($uname&&trim($uname)!='')
{
$rs = M(MODULE_NAME)->where("uname='".$uname."' and id <> ".intval($_REQUEST['id']))->count();
if($rs >0)
{
$this->error(l('UNAME_EXISTS'));
}
}
}
public function toogle_status()
{
$id = intval($_REQUEST['id']);
$ajax = intval($_REQUEST['ajax']);
$field = $_REQUEST['field'];
$info = $id.'_'.$field;
$c_is_effect = M(MODULE_NAME)->where('id='.$id)->getField($field);
$n_is_effect = $c_is_effect == 0 ?1 : 0;
M(MODULE_NAME)->where('id='.$id)->setField($field,$n_is_effect);
save_log($info.l('SET_EFFECT_'.$n_is_effect),1);
$this->ajaxReturn($n_is_effect,l('SET_EFFECT_'.$n_is_effect),1)	;
}
}
?>